/*
This is an old version of UserInterface 
This file is obsolete and was replaced with UserInterface.java
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;


@SuppressWarnings("serial")
public class Program extends JFrame 
{
   private static final int WIDTH = 400;
   private static final int HEIGHT = 200;
   
   private JFrame frame;
   private JLabel latitudeL, longitudeL, dateL, timezoneL; 
   private JTextField latitudeTF, longitudeTF; 
   private JButton drawB, exitB, findB, closeB; 
   private JTextArea topText;
   private JComboBox<String> monthCB, dayCB, yearCB, timezoneCB; //drop down
   private DefaultComboBoxModel<String> dayCBM;
   private JCheckBox daylightcb;
   private JMenuBar menuBar;
   private JMenu menu;
   private JMenuItem menuItem;
   
   Calendar today;
   
   //Button handlers
   private DrawButtonHandler drawHandler;
   private ExitButtonHandler exitHandler;
   private FindButtonHandler findHandler;
   private CloseButtonHandler closeHandler;
   
   private ResetMenuHandler resetHandler;
   private DayHandler dayHandler;
   
   private String[] months = {"January","February","March","April","May","June","July",
      "August","September","October","November","December"};
   private String[] days,years;
   private String[] timezones = {"GMT -11:00","GMT -10:00", "GMT -9:00","GMT -8:00", 
      "GMT -7:00","GMT -6:00", "GMT -5:00","GMT -4:30","GMT -4:00","GMT -3:30", "GMT -3:00","GMT -2:00", 
      "GMT -1:00","GMT","GMT +1:00","GMT +2:00", "GMT +3:00", "GMT +3:30","GMT +4:00", "GMT +4:30", 
      "GMT +5:00", "GMT +5:30", "GMT +5:45","GMT +6:00","GMT +6:30", "GMT +7:00","GMT +8:00", 
      "GMT +9:00","GMT +9:30","GMT +10:00", "GMT +11:00","GMT +12:00"};
   
   private double[] standardMeridian = {-165,-150,-135,-120,-105,-90,-75,-60,-60,-45,-45,-30,-15,0,
	   15,30,45,45,60,60,75,75,75,90,90,105,120,135,135,150,165,180};
      
   public Program()
   {
      // initializes days and years and gets current date
	  createDates(); 
	  
	  //creating the pane
	  frame = new JFrame("Sundial Creator");
      Container pane = getContentPane();
      pane.setLayout(new GridBagLayout());
      
      menuBar = new JMenuBar();

      //Build the first menu.
      menu = new JMenu("File");
      menuBar.add(menu);
      
      menuItem = new JMenuItem("Reset");
      resetHandler = new ResetMenuHandler();
      menuItem.addActionListener(resetHandler);
      menu.add(menuItem);
      menuItem = new JMenuItem("Exit");
      exitHandler = new ExitButtonHandler();
      menuItem.addActionListener(exitHandler);
      menu.add(menuItem);
      
      GridBagConstraints constraints = new GridBagConstraints();
      
      //giving all the objects values
      topText = new JTextArea("Welcome to the sundial creator.  This program can create "
         + "a   working sundial print out that uses the latitude and longitude   to work properly almost any where.");
      pane.add(topText);
      topText.setLineWrap(true);
      topText.setEditable(false);      
      latitudeL = new JLabel("Latitude: ");
      longitudeL = new JLabel("Longitude: ");
      dateL = new JLabel("Date of use: ");
      timezoneL = new JLabel("Timezone: ");
      
      //setting length of input
      latitudeTF = new JTextField(10);
      longitudeTF = new JTextField(10);
      
      //buttons
      drawB = new JButton("Draw");
      drawHandler = new DrawButtonHandler();
      drawB.addActionListener(drawHandler);
      exitB = new JButton("Exit");
      exitB.addActionListener(exitHandler);
      findB = new JButton("Find my coordinates");
      findHandler = new FindButtonHandler();
      findB.addActionListener(findHandler);
      
      //drop down
      monthCB = new JComboBox<String>(months);
      monthCB.setSelectedIndex(today.get(Calendar.MONTH));
      dayHandler = new DayHandler();
      monthCB.addActionListener(dayHandler);
      dayCBM = new DefaultComboBoxModel(days);
      dayCB = new JComboBox<String>();
      dayCB.setModel(dayCBM);
      dayCB.setSelectedIndex(today.get(Calendar.DATE)- 1);
      yearCB = new JComboBox<String>(years);
      yearCB.addActionListener(dayHandler);
      timezoneCB = new JComboBox<String>(timezones);
      timezoneCB.setSelectedIndex(1);
      daylightcb = new JCheckBox("Daylight Savings");
      
      //adding onto the pane     
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 1;       // y coordinate
      constraints.gridwidth = 1;   //width of area
      pane.add(latitudeL, constraints);
      
      constraints.gridwidth = 3;
      constraints.gridx = 1;       // x coordinate
      constraints.gridy = 1;       // y coordinate
      pane.add(latitudeTF, constraints);
      
      constraints.gridwidth = 1;
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 2;       // y coordinate
      pane.add(longitudeL, constraints);
      
      constraints.gridwidth = 3;
      constraints.gridx = 1;       // x coordinate
      constraints.gridy = 2;       // y coordinate
      pane.add(longitudeTF, constraints);
      
      constraints.gridwidth = 1;
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 3;       // y coordinate
      pane.add(dateL, constraints);
      
      constraints.gridx = 1;       // x coordinate
      constraints.gridy = 3;       // y coordinate
      pane.add(monthCB, constraints);
      
      constraints.gridx = 2;       // x coordinate
      constraints.gridy = 3;       // y coordinate
      pane.add(dayCB, constraints);
      
      constraints.gridx = 3;       // x coordinate
      constraints.gridy = 3;       // y coordinate
      pane.add(yearCB, constraints);
      
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 4;       // y coordinate
      pane.add(timezoneL, constraints);
      
      constraints.gridx = 1;       // x coordinate
      constraints.gridy = 4;       // y coordinate
      pane.add(timezoneCB, constraints);
      
      constraints.gridx = 2;       // x coordinate
      constraints.gridy = 4;       // y coordinate
      pane.add(daylightcb, constraints);
      
      constraints.gridwidth = 2;
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 5;       // y coordinate
      pane.add(drawB, constraints);
      
      constraints.gridwidth = 2;
      constraints.gridx = 2;       // x coordinate
      constraints.gridy = 5;       // y coordinate
      pane.add(exitB, constraints);

      constraints.gridwidth = 4; 
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 6;       // y coordinate
      pane.add(findB, constraints);
      
      constraints.gridwidth = 4;
      constraints.ipady = 5;      //make this component tall
      constraints.gridx = 0;       // x coordinate
      constraints.gridy = 0;       // y coordinate
      pane.add(topText, constraints);
      

      frame.setJMenuBar(menuBar);
      frame.add(pane);
      frame.setLocation(100, 100);
      frame.setSize(WIDTH, HEIGHT);
      frame.setVisible(true);
      frame.pack();
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
 
   private void createDates()
   {
	   today = Calendar.getInstance();
	   int y;
	   // days per month
	   switch(today.get(Calendar.MONTH)) {
	      case 3:
	   	  case 5:
	   	  case 8:
	   	  case 10:
	   		  y = 30;
	   		  break;
	   	  case 1:
	   		  if (today.get(Calendar.YEAR) % 4 == 0)
	   			  y = 29; //leap year
	   		  else
	   			  y = 28;
	   		  break;
	   	  default: 
	   		  y = 31;
	   		  break;
	   }
	   
	   days = new String[y];	   
	   for (int i = 0; i < y; i++)
	   {
		   days[i] = String.valueOf(i+1);
	   }
	   
	   int temp = today.get(Calendar.YEAR);
	   years = new String[10];
	   for (int i = 0; i < 10; i++) {
		   years[i] = String.valueOf(temp++);
	   }
   }
   
   private class DrawButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         double latitude, longitude;
         int month, day, year;
         
         try {
        	 latitude = Double.parseDouble(latitudeTF.getText());
        	 longitude = Double.parseDouble(longitudeTF.getText());
        	 month = monthCB.getSelectedIndex() + 1;
             day = dayCB.getSelectedIndex() + 1;
             year = yearCB.getSelectedIndex() + today.get(Calendar.YEAR);
             
             double std_mer = standardMeridian[timezoneCB.getSelectedIndex()];
             
             Calculation c = new Calculation(latitude, longitude, year, month, day, std_mer);
     		
             draw(c);
         }
         catch (NumberFormatException e) {
        	 	JOptionPane.showMessageDialog(frame, "Error! Latitude and Longitude must be a number.", "Invalid Input", JOptionPane.ERROR_MESSAGE);

        }
         
      }
   }
   
   private void draw(Calculation degree)
   {
   }
   
   public class DayHandler implements ActionListener
   {
	   public void actionPerformed(ActionEvent event) 
	   {
		   int y;
		   switch(monthCB.getSelectedIndex()) {
		      case 3:
		   	  case 5:
		   	  case 8:
		   	  case 10:
		   		  y = 30;
		   		  break;
		   	  case 1:
		   		  if ((today.get(Calendar.YEAR) + yearCB.getSelectedIndex() ) % 4 == 0)
		   			  y = 29;
		   		  else
		   			  y = 28;
		   		  break;
		   	  default: 
		   		  y = 31;
		   		  break;
		   }
		   days = new String[y];	   
		   for (int i = 0; i < y; i++)
		   {
			   days[i] = String.valueOf(i+1);
		   }
		   int temp = dayCB.getSelectedIndex();
		   dayCBM = new DefaultComboBoxModel<String>(days);
		   dayCB.setModel(dayCBM);
		   if (y > temp)  //max days > old day selected
			   dayCB.setSelectedIndex(temp);
		   else
			   dayCB.setSelectedIndex(y-1);
	   }  
   } 
   
   public class ResetMenuHandler implements ActionListener
   {
	   public void actionPerformed(ActionEvent event) 
	   {  
		   latitudeTF.setText("");
		   longitudeTF.setText("");
		   monthCB.setSelectedIndex(today.get(Calendar.MONTH) + 1);
		   dayCB.setSelectedIndex(today.get(Calendar.DATE) - 1);
		   yearCB.setSelectedIndex(0);
		   timezoneCB.setSelectedIndex(2);
		   daylightcb.setSelected(false);
	   }  
   }
   
   public class ExitButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }
   
   public class CloseButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }
   
   public class FindButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         try {
            Desktop.getDesktop().browse(new URI("http://universimmedia.pagesperso-orange.fr/geo/loc.htm"));
         } 
         catch (URISyntaxException | IOException ex) {
           //It looks like there's a problem
         }
      }
   }
   
   public static void main(String[] args)
   {
      Program interfaceObj = new Program();
   }
}
